<template>
  <div class="login-pane">
    <h1 class="title">后台管理系统</h1>
    <el-tabs type="border-card" stretch v-model="currentTab">
      <el-tab-pane name="account">
        <template #label>
          <span class="align-center">
            <el-icon><Avatar /></el-icon><span>帐号登录</span>
          </span>
        </template>
        <LoginAccount ref="accountRef" />
      </el-tab-pane>
      <el-tab-pane name="phone">
        <template #label>
          <span class="align-center">
            <el-icon><Cellphone /></el-icon><span>手机登录</span>
          </span>
        </template>
        <LoginPhone ref="phoneRef" />
      </el-tab-pane>
    </el-tabs>
    <div class="account-control">
      <el-checkbox v-model="isKeepPassword">记住密码</el-checkbox>
      <el-link type="primary">忘记密码</el-link>
    </div>
    <el-button type="primary" class="login-btn" @click="handleLoginClick"
      >立即登录</el-button
    >
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import LoginAccount from './LoginAccount.vue'
import LoginPhone from './LoginPhone.vue'
import { LOGINTYPE } from '../types'

export default defineComponent({
  components: { LoginAccount, LoginPhone },
  setup() {
    // 是否保持登录
    const isKeepPassword = ref(true)
    // 账户登录组件的Ref对象
    const accountRef = ref<InstanceType<typeof LoginAccount>>()
    // 手机登录组件的Ref对象
    const phoneRef = ref<InstanceType<typeof LoginPhone>>()
    // 登录类型
    const currentTab = ref<string>(LOGINTYPE[0])
    // 定义方法
    const handleLoginClick = () => {
      currentTab.value === LOGINTYPE[0]
        ? accountRef.value?.loginAction(isKeepPassword.value)
        : phoneRef.value?.loginAction()
    }
    return {
      isKeepPassword,
      handleLoginClick,
      accountRef,
      phoneRef,
      currentTab
    }
  }
})
</script>

<style lang="scss" scoped>
.login-pane {
  width: 320px;
  margin-bottom: 120px;
  .title {
    text-align: center;
  }
  .account-control {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .login-btn {
    width: 100%;
    margin-top: 10px;
  }
}
</style>
